package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.mapper.AppUserMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppLevelRateBo;
import com.ruoyi.app.domain.vo.AppLevelRateVo;
import com.ruoyi.app.domain.AppLevelRate;
import com.ruoyi.app.mapper.AppLevelRateMapper;
import com.ruoyi.app.service.IAppLevelRateService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * APP手续费费率Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-23
 */
@RequiredArgsConstructor
@Service
public class AppLevelRateServiceImpl implements IAppLevelRateService {


    private final AppUserMapper appUserMapper;
    private final AppLevelRateMapper baseMapper;


    /**
     * 查询APP手续费费率
     */
    @Override
    public AppLevelRateVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询APP手续费费率列表
     */
    @Override
    public TableDataInfo<AppLevelRateVo> queryPageList(AppLevelRateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppLevelRate> lqw = buildQueryWrapper(bo);
        Page<AppLevelRateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询APP手续费费率列表
     */
    @Override
    public List<AppLevelRateVo> queryList(AppLevelRateBo bo) {
        LambdaQueryWrapper<AppLevelRate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public BigDecimal userRate(Long id) {
        AppUserVo userVo = appUserMapper.selectVoById(id);
        List<AppLevelRateVo> rateVos = queryList(new AppLevelRateBo());
        BigDecimal rate = BigDecimal.ZERO;
        for (AppLevelRateVo rateVo : rateVos) {
            if (userVo.getLevel().equals(rateVo.getLevelId())) {
                rate = rateVo.getNumber();
                break;
            }
        }
        return rate;
    }

    private LambdaQueryWrapper<AppLevelRate> buildQueryWrapper(AppLevelRateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppLevelRate> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLevelId() != null, AppLevelRate::getLevelId, bo.getLevelId());
        lqw.eq(bo.getNumber() != null, AppLevelRate::getNumber, bo.getNumber());
        return lqw;
    }

    /**
     * 新增APP手续费费率
     */
    @Override
    public Boolean insertByBo(AppLevelRateBo bo) {
        AppLevelRate add = BeanUtil.toBean(bo, AppLevelRate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP手续费费率
     */
    @Override
    public Boolean updateByBo(AppLevelRateBo bo) {
        AppLevelRate update = BeanUtil.toBean(bo, AppLevelRate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppLevelRate entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP手续费费率
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
